<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserMentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user=\Cookie::get('user');
        $orders=\DB::table("orders")->where("uid",$user['id'])->where("status","5")->get();
        foreach($orders as $k=>$v){
            $goods=\DB::table("goods")->where("id",$v['gid'])->first();
            $orders[$k]['gimg']=$goods['img'];
            $orders[$k]['title']=$goods['title'];
        }
        return view("home.user.usercomment",['orders'=>$orders]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$request->except("_token","code");

        if($data['text']==""){
            return back();
        }
        $user=\Cookie::get('user');
        $data['uid']=$user['id'];
        $data['createtime']=time();
        $res=\DB::table("comment")->insert($data);
        if($res){
            \DB::table("orders")->where("code",$request->input("code"))->where("gid",$data['gid'])->update(["comment"=>1]);
        }
        return redirect("/userment");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function add($gid,$code)
    {
       return view("home.user.comment")->with("gid",$gid)->with("code",$code);
    }
}
